<template>
  <div class="serverConfig">
    <div class="page-head">
      <div class="page-bread">
        <div class="bread-prev" @click="back">数据模板</div>
        <div class="bread-divider">/</div>
        <div class="bread-now">计算点位配置</div>
      </div>
      <div class="bread-info">
        <span>业务模板名称：{{$route.query.name}}</span>  
        <span class="ml15"> CNC类型：{{$route.query.cnc_name}}</span>
      </div>
      <div class="page-tool">
        <search-input placeholder="点位名称" @search="searchChange" />
        <div class="tool-btn" @click="open('calculationAdd')">新增点位</div>
      </div>
    </div>
    <div class="page-body">
      <div class="table_data">
        <el-table :data="tableData" stripe border :fit="true" :height="$tableHeight">           
          <el-table-column
            prop="name"
            :show-overflow-tooltip="true"
            label="点位名称"
          >
          </el-table-column>
          <el-table-column
            prop="source"
            :show-overflow-tooltip="true"
            label="数据来源"
          >
            <template slot-scope="{ row }">
              <div>
                {{sources.find(s => s.k == row.source).v}}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="calculate_type"
            :show-overflow-tooltip="true"
            label="计算类型"
          >
            <template slot-scope="{ row }">
              <div>
                {{calculate_typeSel.find(s => s.k == row.calculate_type).v}}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="formula"
            :show-overflow-tooltip="true"
            label="计算公式"
          >
          </el-table-column>
          <el-table-column
            prop="remark"
            :show-overflow-tooltip="true"
            label="备注"
          >
          </el-table-column>
          <el-table-column label="操作" align="center" width="120">
            <template slot-scope="{ row }">
              <div class="operation-col">
                <div class="txt-btn ope-btn" @click="open('calculationUpdate', row)">
                  <el-tooltip
                    effect="dark"
                    content="修改"
                    placement="top-start"
                  >
                    <img src="~assets/img/edit.png" alt="" />
                  </el-tooltip>
                </div>
                <div class="txt-btn" @click="doDelete(row)">
                  <el-tooltip
                    effect="dark"
                    :content="row.isuse == 0 ? '删除' : '该条数据已被使用，不可删除'"
                    placement="top-start"
                  >
                    <img src="~assets/img/delete.png" :class="{'disable-icon': row.isuse == 1}" />
                  </el-tooltip>
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="page-footer up">
        <pagina-tion
          class="page-pager"
          v-show="total >= 0"
          :background="true"
          :total="total"
          :offset.sync="offset"
          :limit.sync="limit"
          @pagination="getChangePage"
          @sizeChange="getSizePage"
        />
      </div>
    </div>

    <!-- 新增参数弹窗 -->
    <el-dialog
      :visible.sync="model.visible"
      width="370px"
      :title="model.type === 'calculationAdd' ? '新增计算点位' : '修改计算点位'"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <div class="dialog-body">
        <el-form
          ref="form"
          :model="form"
          label-width="110px"
          label-position="left"
        >
          <el-form-item label="点位名称" class="required">
            <el-input
              name="pointName"
              placeholder="请输入点位名称"
              v-model="form.name"
              v-validate="'required'"
            />
            <div v-show="errors.has('pointName')" class="el-form-item__error">
              {{ errors.first("pointName") }}
            </div>
          </el-form-item>
          <el-form-item label="数据来源" label-position="left" class="required">
            <el-select
              v-model="form.source"
              name="source"
              placeholder="请选择数据来源"
              v-validate="'required'"
            >
              <el-option
                v-for="item in sources"
                :key="item.k"
                :label="item.v"
                :value="item.k"
              >
              </el-option>
            </el-select>
            <div v-show="errors.has('source')" class="el-form-item__error">
              {{ errors.first("source") }}
            </div>
          </el-form-item>
          <el-form-item label="计算类型" label-position="left" class="required">
            <el-select
              v-model="form.calculate_type"
              name="calculate_type"
              placeholder="请选择计算类型"
              v-validate="'required'"
            >
              <el-option
                v-for="item in calculate_typeSel"
                :key="item.k"
                :label="item.v"
                :value="item.k"
              >
              </el-option>
            </el-select>
            <div v-show="errors.has('calculate_type')" class="el-form-item__error">
              {{ errors.first("calculate_type") }}
            </div>
          </el-form-item>
          <el-form-item label="计算公式" class="required">
            <el-input
              name="formula"
              placeholder="请输入计算公式"
              v-model="form.formula"
              v-validate="'required'"
            />
            <div v-show="errors.has('formula')" class="el-form-item__error">
              {{ errors.first("formula") }}
            </div>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" type="textarea" maxlength="128" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 删除弹窗 -->
    <el-dialog
      :visible.sync="visibility"
      width="430px"
      height="100px"
      :show-close="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div class="dialog-content comfirm-body">
        <img class="comfirm-ico" src="~assets/img/warning.png" />
        <div class="comfirm-txt">
          <div>确定删除该条数据？</div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="visibility = false">取 消</el-button>
        <el-button type="primary" @click="asyncDelete">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>


<script>
import * as api from "@/api"
import { limit, smallLimit } from "@/consts/consts";
import { sources } from "@/consts/codesMap";
import { calacKey } from "@/consts/formKey";
import searchInput from "@/components/libs/searchInput";
import paginaTion from "@/components/libs/paginaTion";

export default {
  components: {
    searchInput,
    paginaTion,
  },

  data() {
    return {
      limit,
      total: 0,
      offset: 0,
      calculate_typeSel: [],
      form: {
        ...calacKey,
      },
      tableData: [],
      search: '',
      visibility: false,
      sources,
      model: {
        type: "calculationAdd",
        visible: false,
      },
    };
  },

  methods: {
    back() {
      this.$router.go(-1);
    },
    searchChange(e) {
      this.search = e;
      this.offset = 0;
      this.getList();
    },
    close() {
      this.model.visible = false;
      this.form = { ...calacKey };
    },

    getChangePage(val) {
      this.offset = val.offset;
      this.getList();
    },
    
    getSizePage(val) {
      this.offset = 0
      this.getList();
    },

    submit() {
      this.$validator.validateAll().then((valid) => {
        if (valid) {
          this.addOrEdit();
        } else {
          this.$message.warning("有信息未填写正确");
        }
      });
    },

    open(type, row = null) {
      this.$validator.reset();
      this.model.type = type;
      if (row) {
        for (const k in calacKey) {
          this.form[k] = row[k];
        }
        this.id = row.id;
      }

      this.model.visible = true;
    },

    doDelete(row) {
      if (row.isuse == 0) {
        this.id = row.id;
        this.visibility = true;
      }
    },

    // 下拉列表
    async downList(type) {
      const { code, data } = await api.dropdown(this).downList({
        type,
      });
      if (code === 0) {
        this[`${type}Sel`] = data;
        this.getList();
      }
    },

    async getList() {
      const { code, data, total } = await api.template(this).calculationList({
        template_id: this.$route.query.id,
        offset: this.offset,
        limit: this.limit,
        name: this.search
      });
      if (code === 0) {
        this.tableData = data
        this.total = total
      }
    },
    async addOrEdit() {
      let fn = this.model.type;
      const { code } = await api.template(this)[fn]({
        template_id: this.$route.query.id,
        ...this.form
      }, this.id)
      if (code === 0) {
        this.$message.success("操作成功");
        this.close();
        this.getList();
      }
    },
    
    async asyncDelete() {
      const { code } = await api.template(this).calculationDelete(this.id);
      if (code === 0) {
        this.$message.success("操作成功");
        this.id = "";
        this.visibility = false;
        if (this.total % this.limit === 1) {
          if (this.offset >= this.limit) {
            this.offset -= this.limit;
          }
        }
        this.getList();
      }
    },
  },
  created () {
    this.downList('calculate_type')
  },
};
</script>

<style>
</style>